<?php
	@header("Content-type:text/html; charset=utf-8"); // 编辑器的编码格式也必须设置为utf-8, @表示忽略此处可能产生的告警或错误
?>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!编辑器的编码格式也必须设置为utf-8>
<title>迷你订餐系统</title>
<script type="text/javascript">
function todayshow() {
    var objDate = new Date();
	var day = objDate.getDay();
	var arrDayMap = new Array(7);
	arrDayMap[0] = "星期日";
	arrDayMap[1] = "星期一";
	arrDayMap[2] = "星期二";
	arrDayMap[3] = "星期三";
	arrDayMap[4] = "星期四";
	arrDayMap[5] = "星期五";
	arrDayMap[6] = "星期六";
	
	var formmonth = objDate.getMonth() + 1;
	var formtoday = objDate.getDate();
	var formhour = objDate.getHours();
	var formminute = objDate.getMinutes();
	if(formmonth < 10) formmonth = "0" + formmonth;
    if(formtoday < 10) formtoday = "0" + formtoday;
	if(formhour < 10) formhour = "0" + formhour;
	if(formminute < 10) formminute = "0" + formminute;
	var formdate = objDate.getFullYear() + "-" + formmonth + "-" + formtoday;
	var formtime = formhour + ":" + formminute;
 	return arrDayMap[day] + "  " + formdate + "  " + formtime;
}

function oncount(obj, val) { 
    var objBill = document.getElementById("bill");
    if (obj.checked) {	
        objBill.value = parseInt(objBill.value) + val;
    } else {
	    objBill.value = parseInt(objBill.value) - val;	
	}
}

function onclear() {
    var objBill = document.getElementById("bill");
	objBill.value = 0;
    var objMenu = document.getElementsByName("menu[]");     
    var len = objMenu.length;     
    for (var i=0; i<len; i++) {                    
        objMenu[i].checked = false;
    }	 
}

function validate() {
    var objBill = document.getElementById("bill");
	if (parseInt(objBill.value) == 0) {
	    return confirm("对不起，您没有点餐。如已定过餐继续提交会取消您上次的订餐，？");
	}
	
	if ((parseInt(objBill.value) > 0) && (parseInt(objBill.value) < 8)) {
		alert("定餐金额不能少于8元！");
		return false;
	} 
}

function getCookie(c_name) {
	if(document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if(c_start != -1) { 
			c_start = c_start + c_name.length + 1;
			var c_end = document.cookie.indexOf(";", c_start);
			if(c_end == -1) {
				c_end = document.cookie.length;
			}
			
			return document.cookie.substring(c_start, c_end);
        } 
    }

    return "";
}
</script>
</head>

<body>
<div align="center">
    <table width="640"><tr align="center"><td width="30%"><a href="../newmenu/newmenu.php">不吃快餐吃粉面</a><br /><a href="../menulrj/srcmenu.php">不吃快餐吃卤肉卷</a></td><td width="40%">
    <b>翠竹亭今日菜单</b>
	<br /><br />					 
    <script type="text/javascript">
    document.write(todayshow())
    </script>
	<br /><br />
	</td><td width="30%"><a href='../minimenu/srcmenu.php'>切换到新版</a></td><tr><table>
	
	<form method="post" action="destmenu.php" onsubmit="return validate()">
	<table width="640" border="0">
	<tr align="center"><td>五元</td><td>四元</td><td>三元</td><td>二元</td><td>一元</td></tr>
	</table>
	<hr width='680' />
	
<?php
	// 从配置文件中解析今日菜单
	$arr = file("srcmenu.ini"); // 返回分行数组
	$totallines = count($arr); // 行数
	$todaymenu = array(array(), array(), array(), array(), array(), array()); // 菜品分一到五元，另有零元为免费例汤或其它赠送
	$istodaymenu = false;
	@$sectionname = "[" . date("l") . "]"; // @表示忽略此处可能产生的告警或错误(为date("l"))
	for($i=2; $i<$totallines; $i++) { // 跳过开始两行的快餐店信息Section(内部的三个逻辑相关的if语句也可改为if...else if...else...形式)
		if(stristr($arr[$i], $sectionname)) {
			$istodaymenu = true;
			continue;
		}
		
		if(stristr($arr[$i], "[") && $istodaymenu) {
			break;
		}
		
		if($istodaymenu) {
			$separator = strpos($arr[$i], "=");
			$name = substr($arr[$i], 0, $separator);
			$price = substr($arr[$i], $separator+1);
			trim($name);
			trim($price);
			$price = intval($price);
			if(($price < 0) || ($price > 5)) { // 本菜单目前只支持一到五元的菜品(以及零元的免费例汤等)，此处旨在屏蔽用户在配置文件中书写错误而导致程序问题
				continue;
			}
			
			$todaymenu[$price][] = $name; // array_push($todaymenu[$price], $name)效率低于此句
		}
	}
	
	// 构造显示今日菜单
	echo "<table width='640' border='0'>";
	echo "<tr align='center'>";
	$count1 = count($todaymenu);
	for($i=$count1-1; $i>0; $i--) { // 分别处理五元到一元(解析数组的5到1索引)的菜单，零元(0索引)通常为免费例汤而放在最后处理
		echo "<td valign='top'><table width='128'>";
		$count2 = count($todaymenu[$i]);
		for($j=0; $j<$count2; $j++) {
		    $wordcount = strlen($todaymenu[$i][$j]) / 3; // utf-8每个汉字占3个字节，而显示时界面只占两个字符宽度，因此对齐时一个汉字要补多一个字节(如果菜名中意外出现非汉字，则显示宽度会长出一个字符)
			$val = str_pad($todaymenu[$i][$j], 12+$wordcount, "-") . $i . "元"; // 菜名长度暂定为7个汉字内，即界面显示对齐为14个字符宽度(如果意外超出则均会紧挨着价格显示)
			//$sel = '<input type="checkbox" name="menu[]" value="$val" onclick="oncount(this, $i)">'; // 单引号内的双引号内变量在此被当作普通字符，下步无法再解析，因此达不到需求
			$sel = "<input type='checkbox' name='menu[]' value='$val' onclick='oncount(this, $i)'>"; // 双引号内的单引号内变量在此被解析	
			echo "<tr align='left'><td>" . $sel . $todaymenu[$i][$j] . "</td></tr>";
		}
		
		echo "</table></d>";
	}

	echo "</tr></table>";
	echo "<hr width='680' />";
	$freecount = count($todaymenu[0]);
	for($i=0; $i<$freecount; $i++) {
		echo " 赠送:" . $todaymenu[0][$i];
	}
?>
	
	<br /><br />
	<input type="button" value="清空" onclick="onclear()" />
	<select id="name" name="name">
	
<?php
    $userlist = file("userlist.ini") or die("Unable to open username config!"); // 返回文件分行内容数组，这里即用户名
	$usercount = count($userlist); // 返回文件行数，这里即用户数
	for($i=0; $i<$usercount; $i++) {
		$userlist[$i] = rtrim($userlist[$i]); // 删除末尾的特殊字符
		echo "<option value='$userlist[$i]'>$userlist[$i]</option>";
	}
?>

	</select>
	<input type="text" size="4" readonly="readonly" id="bill" name="bill" value="0" />
	<input type="submit" value="提交" />
    <a href="destmenu.php">查看全部定餐结果</a>
	</form>
	<script type="text/javascript">
	document.all.name.selectedIndex = parseInt(getCookie("selectedIndex")) // 等于document.getElementById("name").selectedIndex = parseInt(getCookie("selectedIndex"))
    </script>
</div>
</body>
</html>
